package org.vnl.platform.web.util;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import org.vnl.platform.job.JobExecException;
import org.vnl.platform.job.JobExecuteEngine;

/**
 * 用於當應用程式起動時,先行執行一些需初始化的工作,只接受JOBS_META這個參數,其內容為一個Job的設定XML,請參考Job的設定文件
 * 
 * @author Vaughan
 *
 */
public class StartUpServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = -7798921485482600041L;
    
    private static final String JOBS_META = "JOBS_META";

    @Override
    public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
        arg1.getWriter().write("");
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        String meta = config.getInitParameter(JOBS_META);
        JobExecuteEngine engine = new JobExecuteEngine();
        try {
            engine.init(meta);
            engine.startEngine();
        } catch (JobExecException e) {
            e.printStackTrace();
            System.out.println("================================================================================================");
            System.out.println("StartUp Servlet start with error!"); 
            System.out.println("================================================================================================");
        }
        System.out.println("================================================================================================");
        System.out.println("StartUp Servlet start successfully!"); 
        System.out.println("================================================================================================");
    }

}
